package model;

import buffer.Buffer;
import buffer.BufferCollection;

/**
 * The {@link Variable} class can distribute each value that is put in it to all its output buffers.
 */
public class Variable<T> implements HasSource<T>{
	
	private final BufferCollection<T> outputBuffers;

	private BufferCollection<T> getOutputBuffers() {
		return outputBuffers;
	}
	
	/**
	 * Constructor of {@link Variable}.
	 */
	public Variable() {
		this.outputBuffers = new BufferCollection<>();
	}
	
	@Override
	public Buffer<T> getSource() {
		final Buffer<T> result = new Buffer<T>();
		this.getOutputBuffers().add(result);
		return result;
	}	
	
	/**
	 * Distributes the provided value to all output buffers of this {@link Variable}.
	 * @param value is the value that is put in this {@link Variable}
	 */
	public void put(final T value){
		this.getOutputBuffers().put(value);
	}

	@Override
	public void accept(final HasSourceVisitor<T> visitor) {
		visitor.visit(this);
	}
}
